package com.ruoyi.cngj.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cngj.domain.CngjEnterprise;
import com.ruoyi.cngj.service.ICngjEnterpriseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 企业情况展示Controller
 * 
 * @author yuexl
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/cngj/enterprise")
public class CngjEnterpriseController extends BaseController
{
    private String prefix = "cngj/enterprise";

    @Autowired
    private ICngjEnterpriseService cngjEnterpriseService;

    //@RequiresPermissions("cngj:enterprise:view")
    @GetMapping()
    public String enterprise()
    {
        return prefix + "/enterprise";
    }

    /**
     * 查询企业情况展示列表
     */
    //@RequiresPermissions("cngj:enterprise:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CngjEnterprise cngjEnterprise)
    {
        startPage();
        List<CngjEnterprise> list = cngjEnterpriseService.selectCngjEnterpriseList(cngjEnterprise);
        return getDataTable(list);
    }

    /**
     * 导出企业情况展示列表
     */
    //@RequiresPermissions("cngj:enterprise:export")
    @Log(title = "企业情况展示", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CngjEnterprise cngjEnterprise)
    {
        List<CngjEnterprise> list = cngjEnterpriseService.selectCngjEnterpriseList(cngjEnterprise);
        ExcelUtil<CngjEnterprise> util = new ExcelUtil<CngjEnterprise>(CngjEnterprise.class);
        return util.exportExcel(list, "企业情况展示数据");
    }

    /**
     * 新增企业情况展示
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存企业情况展示
     */
    //@RequiresPermissions("cngj:enterprise:add")
    @Log(title = "企业情况展示", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CngjEnterprise cngjEnterprise)
    {
        return toAjax(cngjEnterpriseService.insertCngjEnterprise(cngjEnterprise));
    }

    /**
     * 修改企业情况展示
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CngjEnterprise cngjEnterprise = cngjEnterpriseService.selectCngjEnterpriseById(id);
        mmap.put("cngjEnterprise", cngjEnterprise);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业情况展示
     */
    //@RequiresPermissions("cngj:enterprise:edit")
    @Log(title = "企业情况展示", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CngjEnterprise cngjEnterprise)
    {
        return toAjax(cngjEnterpriseService.updateCngjEnterprise(cngjEnterprise));
    }

    /**
     * 删除企业情况展示
     */
    //@RequiresPermissions("cngj:enterprise:remove")
    @Log(title = "企业情况展示", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cngjEnterpriseService.deleteCngjEnterpriseByIds(ids));
    }

    /**
     * 改变位置
     */
    @Transactional
    @PostMapping("/enterpriseMove")
    @ResponseBody
    public AjaxResult enterpriseMove(String type,String id,String changeId)
    {
        CngjEnterprise cngjEnterprise = cngjEnterpriseService.selectCngjEnterpriseById(id);
        CngjEnterprise changeCngjEnterprise = cngjEnterpriseService.selectCngjEnterpriseById(changeId);
        int num =0;
        if("1".equals(type) || "2".equals(type)){
            //上移
            int currSort = cngjEnterprise.getEnterpriseSort();
            cngjEnterprise.setEnterpriseSort(changeCngjEnterprise.getEnterpriseSort());
            changeCngjEnterprise.setEnterpriseSort(currSort);
            num +=cngjEnterpriseService.updateCngjEnterprise(cngjEnterprise);
            num +=cngjEnterpriseService.updateCngjEnterprise(changeCngjEnterprise);
        }
        if("3".equals(type)){
            cngjEnterprise.setEnterpriseSort(Integer.parseInt(changeId)+1);
            num +=cngjEnterpriseService.updateCngjEnterprise(cngjEnterprise);
        }
        return toAjax(num);
    }

    /**
     * 启用停用企业风采素材
     */
    @Log(title = "启用停用企业风采素材")
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id,String enterpriseState)
    {
        CngjEnterprise cngjEnterprise = new CngjEnterprise();
        cngjEnterprise.setId(id);
        cngjEnterprise.setEnterpriseState(enterpriseState);
        return toAjax(cngjEnterpriseService.updateCngjEnterprise(cngjEnterprise));
    }

}
